<?php
/**
 * 管理菜单函数
 *
 * @version        $Id: inc_menu_func.php 1 10:32 2010年7月21日Z tianya $
 * @package        DedeCMS.Administrator
 * @copyright      Copyright (c) 2007 - 2010, DesDev, Inc.
 * @license        http://help.dedecms.com/usersguide/license.html
 * @link           http://www.dedecms.com
 */
require_once(dirname(__FILE__)."/../config.php");
require_once(DEDEINC."/dedetag.class.php");

function GetMenus($userrank,$topos='main')
{
    global $openitem,$headTemplet,$footTemplet,$itemTemplet;
    if($topos=='main')
    {
        $openitem = (empty($openitem) ? 1 : $openitem);
        $menus = $GLOBALS['menusMain'];
    }
    else if($topos=='module')
    {
        $openitem = 100;
        $menus = $GLOBALS['menusMoudle'];
    }
    $dtp = new DedeTagParse();
    $dtp->SetNameSpace('m','<','>');
    $dtp->LoadSource($menus);
    $dtp2 = new DedeTagParse();
    $dtp2->SetNameSpace('m','<','>');
    $m = 0;
    $menu_arrs=[];
    foreach($dtp->CTags as $i=>$ctag)
    {
        $menu_arr=[];
        if($ctag->GetName()=='top' && ($ctag->GetAtt('rank')=='' || TestPurview($ctag->GetAtt('rank')) ))
        {
            if($openitem!=999 && !preg_match("#".$openitem.'_'."#", $ctag->GetAtt('item')) && $openitem!=100) continue;
            $m++;
//            echo "<!-- Item ".($m+1)." Strat -->\r\n";
//            $htmp = str_replace("~channelname~",$ctag->GetAtt("name"),$headTemplet);
//            if(empty($openitem) || $openitem==100)
//            {
//                if($ctag->GetAtt('notshowall')=='1') continue;
//                $htmp = str_replace('~display~',$ctag->GetAtt('display'),$htmp);
//            }
//            else
//            {
//                if($openitem==$ctag->GetAtt('item') || preg_match("#".$openitem.'_'."#", $ctag->GetAtt('item')) || $openitem=='-1')
//                    $htmp = str_replace('~display~','block',$htmp);
//                else
//                    $htmp = str_replace('~display~','none',$htmp);
//            }
//            $htmp = str_replace('~cc~',$m.'_'.$openitem,$htmp);
            $menu_arr['menu']=$ctag->GetAtt("name");
//            echo $htmp;
            $dtp2->LoadSource($ctag->InnerText);
            foreach($dtp2->CTags as $j=>$ctag2)
            {
                $ischannel = trim($ctag2->GetAtt('ischannel'));
                if($ctag2->GetName()=='item' && ($ctag2->GetAtt('rank')=='' || TestPurview($ctag2->GetAtt('rank')) ) )
                {

                    $menu_arr['sub_menu'][]=[
                        'menu'=>$ctag2->GetAtt("name"),
                        'url'=>$ctag2->GetAtt('link'),
                    ];
                }
            }
        }
        $menu_arrs[]=$menu_arr;
    }
    //返回菜单
    /**
    [{
    'path': '/core',
    'icon': 'android-checkbox',
    'name': 'core',
    'title': '常用操作',
    'children': [
    {'path': 'content-main','title': '网站栏目管理2', 'name': 'content-main?consti', 'icon': 'compose' },
    {'path': 'content-list', 'title': '所有档案列表', 'name': 'content-list', 'icon': 'compose'}
    ]
    }]
     */

     $menus=[];
     if($openitem==1){
         $path='core';
     }elseif ($openitem==100){
         $path='model';
     }elseif ($openitem==5){
         $path='makehtml';
     }elseif ($openitem==3){
         $path='co';
     }elseif ($openitem==6){
         $path='member';
     }elseif ($openitem==7){
         $path='templets';
     }
     elseif ($openitem==10){
         $path='sys';
     }
    foreach ($menu_arrs as $k=>$v){
        $menus[$k]['icon']='android-checkbox';
        $menus[$k]['path']='/'.$path;
        $menus[$k]['name']=$path;
        $menus[$k]['icon']='android-checkbox';
        $menus[$k]['title']=$v['menu'];
        if(isset($v['sub_menu'])){
            foreach ($v['sub_menu'] as $key=>$menu){
                $url=explode('?',$menu['url']);
                $path=rtrim($url[0],'.php');
                $params=!empty($url[1])?'?'.$url[1]:'';
                $menus[$k]['children'][$key]['icon']='android-checkbox';
                $menus[$k]['children'][$key]['path']='/'.$path;
                $menus[$k]['children'][$key]['name']=$path.$params;
                $menus[$k]['children'][$key]['icon']='compose';
                $menus[$k]['children'][$key]['title']=$menu['menu'];
            }
        }
    }
    return $menus;
}
//End Function